<template name="LausirCodedialog">
	<view v-if="show" class="codedialog">
		<view class="mask"></view>
		<view class="dialog-view">
			<view class="dialog-hd">
				<view class="codedialog-maintitle">
					<text>邀请码</text>
				</view>
			</view>
			<view class="dialog-bd">
				<view class="code-view">
					<view v-for="(code,index) of codeAry" :key="index" class="code-item">{{code}}</view>
				</view>
				<button class="commit" @click="determine">确定</button>
				<div class="close" src="../static/img/close.png" mode="" @click="closeDialog"></div>
			</view>
		</view>

		<view class="keyboard">
			<view class="keyboard-line">
				<view data-val="1" @click="bindKeyEvent" class="button-item">1</view>
				<view data-val="2" @click="bindKeyEvent" class="button-item">2</view>
				<view data-val="3" @click="bindKeyEvent" class="button-item">3</view>
			</view>
			<view class="keyboard-line">
				<view data-val="4" @click="bindKeyEvent" class="button-item">4</view>
				<view data-val="5" @click="bindKeyEvent" class="button-item">5</view>
				<view data-val="6" @click="bindKeyEvent" class="button-item">6</view>
			</view>
			<view class="keyboard-line">
				<view data-val="7" @click="bindKeyEvent" class="button-item">7</view>
				<view data-val="8" @click="bindKeyEvent" class="button-item">8</view>
				<view data-val="9" @click="bindKeyEvent" class="button-item">9</view>
			</view>
			<view class="keyboard-line">
				<view data-val="clear" @click="bindKeyEvent" class="button-item">清空</view>
				<view data-val="0" @click="bindKeyEvent" class="button-item">0</view>
				<view data-val="delete" @click="bindKeyEvent" class="button-item">x</view>
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		name: 'LausirCodedialog',
		props: {
			show: {
				type: Boolean,
				default: false
			},
			len: {
				type: Number,
				default: 8
			}
		},
		data() {
			return {
				codeAry: [],
				currItem: 0,
				countdown: 60,
				cTimer: null,
				callResult: {
					type: 0,
					code: null
				},
				suspend: false
			};
		},
		watch: {
			show: function() {
				if (this.show) {
					if (!this.suspend) {
						this.init();
					}
				} else {
					this.clearCode();
				}
			}
		},
		methods: {
			init: function() {
				var codeAry = [];
				for (var i = 0; i < this.len; i++) {
					codeAry.push('')
				}
				this.codeAry = codeAry;
				this.currItem = 0;
			},
			bindKeyEvent: function(e) {
				var _this = this;
				var val = e.currentTarget.dataset.val;
				switch (val) {
					case "clear":
						_this.clearCode();
						break;
					case "delete":
						_this.deleteCode();
						break;
					default:
						_this.inputVal(val);
						break;
				}
			},
			inputVal: function(val) {
				if (this.currItem < this.len) {
					this.codeAry[this.currItem] = val;
					this.currItem++;
				}
				if (this.currItem == this.len) {
					this.execuCall(1);
				}
			},
			clearCode: function() {
				this.init();
			},
			deleteCode: function() {
				if (this.currItem > 0) {
					this.codeAry[this.currItem - 1] = "";
					this.currItem--;
				}
			},
			determine(){
				this.$emit("Commit");
				let _this = this;
				let inviteCode = this.codeAry.join('')
				this.$post(configUrl.AddCashNum,{
					invite:inviteCode,
					token:uni.getStorageSync("token")
				}).then((res) => {
					console.log(res)
				})
			},
			closeDialog: function() {
				this.$emit("Close");
			},
			getCodeValue: function() {
				var codeStr = "";
				this.codeAry.forEach(function(code) {
					codeStr += code;
				})
				return codeStr;
			},
			execuCall: function(type) {
				this.callResult.type = type;
				if (type == 1) {
					this.callResult.code = this.getCodeValue();
				} else {
					this.suspend = true;
					this.callResult.code = null;
				}
				this.$emit("change", this.callResult);
			},
			resend: function() {
				var _this = this;
				_this.callResult.code = null;
				_this.callResult.type = 0;
				_this.callResult.resendCall = function() {
					_this.init();
				}
				_this.$emit("change", _this.callResult);

			}

		}
	}
</script>

<style scoped lang="scss">
	button::after {
		border: none;
	}

	input {
		outline: none;
		border: none;
		list-style: none;
	}

	.button-item:active {
		background: #d4d4d4;
	}

	.button-item+.button-item {
		border-left: 0.1px solid #d4d4d4;
	}

	.button-item {
		flex: 1;
		padding: 14px 0px;
		text-align: center;
	}

	.keyboard-line+.keyboard-line {
		border-top: 0.1px solid #d4d4d4;
	}

	.keyboard-line {
		display: flex;
	}

	.keyboard {
		background: #fff;
		position: absolute;
		z-index: 999;
		width: 100%;
		left: 0;
		bottom: 0;
		font-size: 17px;
	}

	.dialog-close {
		color: #999;
		height: 20upx;
		width: 20upx;
		font-size: 15upx;
		top: 5upx;
		left: 5upx;
		position: absolute;
	}


	.dialog-close:before {
		content: "\2716";
	}

	.countdown {
		color: #666;
		font-size: 16upx;
	}

	.resend {
		color: #007aff;
		font-size: 16upx;
	}

	.dialog-ft {
		margin-top: 10upx;
	}

	.code-view {
		display: table;
		text-align: center;
		margin: 0 auto;
		border-collapse: separate;
		border-spacing: 10upx 5upx;
	}

	.code-item+.code-item {
		margin-left: 19upx;
	}

	.code-item {
		display: table-cell;
		border-bottom: 1upx solid #999;
		max-width: 35upx;
		min-width: 35upx;
		padding-bottom: 2upx;
		height: 82upx;
		font-family: DINAlternate-Bold;
		font-size: 70upx;
		color: #333333;
		letter-spacing: 0;
		text-align: center;
	}
	.dialog-hd{
		.codedialog-maintitle {
			margin: 60upx auto;
			text {
				font-family: PingFangSC-Regular;
				font-size: 36upx;
				color: #333333;
				letter-spacing: 0.5px;
				text-align: center;
				line-height: 50upx;
			}
		}
	}
	.dialog-bd {
		margin-top: 5upx;

		.commit {
			margin-top: 60upx;
			width: 332upx;
			height: 85upx;
			background: #BA2B2B;
			border-radius: 42.5upx;
			color: #FFFFFF;
		}

		.close {
			position: absolute;
			bottom: -160upx;
			left: 229upx;
			border: none;
			padding: 0;
			width: 92upx;
			height: 92upx;
			// background: darkblue;
			z-index: 99999999;
			background: url("") no-repeat;
		}
	}

	.codedialog-subtitle {
		margin-top: 5upx;
		padding: 5upx 0upx;
		font-size: 15upx;
		line-height: 1.4;
		word-wrap: break-word;
		word-break: break-all;
		color: #999;
	}

	.dialog-view {
		position: fixed;
		z-index: 999;
		width: 550upx;
		height: 457upx;
		/* max-width: 300upx; */
		top: -400upx;
		left: 0;
		right: 0;
		bottom: 0;
		margin: auto;
		background-color: #fff;
		text-align: center;
		border-radius: 3upx;
		overflow: hidden;
		padding: 20upx 10upx;
		border-radius: 12upx;
	}

	.mask {
		position: fixed;
		z-index: 999;
		top: 0;
		right: 0;
		left: 0;
		bottom: 0;
		background: rgba(0, 0, 0, .6);
	}

	.codedialog {
		z-index: 999;
		position: fixed;
		width: 100%;
		height: 100%;
		top: 0;
		left: 0;
		box-sizing: border-box;
	}
</style>
